package com.pan.controller;

import com.google.gson.Gson;
import com.pan.pojo.ResultPojo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/conn")
public class ConnectMethod {

    @PostMapping("/http-stream")
    public void connectHttp(HttpServletRequest request, HttpServletResponse response){

        ResultPojo resultPojo = new ResultPojo();
        try {
            Object result = parseJsonData(request);
            System.out.println(result.toString());
            resultPojo.setResult(result);
            resultPojo.setErrorMsg("errorMsg");
            System.out.println("sleep start");
            Thread.sleep(3000);
            System.out.println("sleep end");
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            // 返回处理结果。
            this.returnResult(response, resultPojo);
        }

    }

    @SuppressWarnings({ "unchecked" })
    private Object parseJsonData(HttpServletRequest request) throws Exception {
        ObjectInputStream in = null;
        Gson gson = new Gson();
        try {
            /*
             * String jodata = request.getParameter("data"); String ss =
             * (String) SerializeUtil.unserialize(jodata.getBytes());
             */
            in = new ObjectInputStream(request.getInputStream());

            String r = (String) in.readObject();
            Map<String, String> data = gson.fromJson(r, HashMap.class);


            // jsonData.setVbdef(data.get("vbdef"));
            Object data1 = data.get("data");
            return data1;
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                }
            }
        }
    }

    /**
     * 返回处理结果。
     *
     * @param response
     *            HTTP响应。
     * @param result
     *            供应链处理结果POJO。
     */
    private void returnResult(HttpServletResponse response, ResultPojo result) {
        OutputStream out = null;
        ObjectOutputStream oos = null;
        Gson gson = new Gson();
        try {
            // 获取数据
            out = response.getOutputStream();
            oos = new ObjectOutputStream(out);
            oos.writeObject(gson.toJson(result));
            oos.flush();
        } catch (Exception e) {
        } finally {
            try {
                if (out != null) {
                    out.close();
                }
                if (oos != null) {
                    oos.close();
                }
            } catch (IOException e) {
            }
        }
    }
}


